<?php

//The helper for HTML tags.  All functions are in alphabetical order

class htmlHelper extends helperBase {
	var $xhtml = true;
	var $opened = array();
	
	function base() {
		return $this->open('base', array(
			'href' => $this->aether->config->get('general', 'domain') . $this->aether->config->get('general', 'webRoot'),
		), array('selfClosing' => true));
	}
	
	function doctype($lang, $version, $mode = STRICT) {
		if($lang==HTML) {
			$this->xhtml = false;
		} else {
			$this->xhtml = true;
		}
		if($lang==HTML&&($version==4||$version==4.01)&&$mode==STRICT) {
			return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'.NL.
				TAB.'"http://www.w3.org/TR/html4/strict.dtd">';
		} else if($lang==HTML&&($version==4||$version==4.01)&&$mode==TRANSITIONAL) {
			return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"'.NL.
				TAB.'"http://www.w3.org/TR/html4/loose.dtd">';
		} else if($lang==HTML&&($version==4||$version==4.01)&&$mode==FRAMESET) {
			return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"'.NL.
				TAB.'"http://www.w3.org/TR/html4/frameset.dtd">';
		} else if($lang==XHTML&&$version==1&&$mode==STRICT) {
			return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'.NL.
				TAB.'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
		} else if($lang==XHTML&&$version==1&&$mode==TRANSITIONAL) {
			return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.NL.
				TAB.'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
		} else if($lang==XHTML&&$version==1&&$mode==FRAMESET) {
			return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"'.NL.
				TAB.'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
		} else if($lang==XHTML&&$version==1.1) {
			return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"'.NL.
				TAB.'"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
		}
	}
	
	function href($label, $args = array()) {
		sins($args['url'], '#');
		$return = '<a href="'.$args['url'].'"';
		if(isset($args['rel'])) {
			$return .= ' rel="'.$args['rel'].'"';
		}
		$return .= '>'.$label.'</a>';
		return $return;
	}
	
	function img($args) {
		if(is_string($args))
			$args = array('src'=>$args);
		sins($args['alt'],'');
		$return = '<img src="app/images/'.$args['src'].'" alt="'.$args['alt'].'" />';
		return $return;
	}
	
	function js($files) {
		if(!is_array($files)) {
			$files = array($files);
		}
		$src = 'js/?';
		$first = true;
		foreach($files as $file) {
			if($first) {
				$first = false;
			} else {
				$src .= '&amp;';
			}
			$src .= 'files[]='.urlencode($file);
		}
		$src .= '&amp;cacheId='.$this->aether->cache->getId();
		return $this->open('script', array(
			'type' => 'text/javascript',
			'src' => $src
		), array('content' => ''));
	}
	
	function script($content) {
		return $this->open('script', array(
			'type' => 'text/javascript'
		), array(
			'content' => '<!-- '.$content.' -->'
		));
	}
	
	function style($files) {
		if(!is_array($files)) {
			$files = array($files);
		}
		$src = 'css/?';
		$first = true;
		foreach($files as $file) {
			if($first) {
				$first = false;
			} else {
				$src .= '&amp;';
			}
			$src .= 'files[]='.urlencode($file);
		}
		$src .= '&amp;cacheId='.$this->aether->cache->getId();
		return $this->open('link', array(
			'type' => 'text/css',
			'rel' => 'stylesheet',
			'href' => $src
		), array('selfClosing' => true));
	}
	
	function open($name, $attr = array(), $args = array()) {
		$args = arrayExtend($args, array(
			'selfClosing' => false
		));

		if(isset($attr['class'])&&$attr['class']==array()) {
			unset($attr['class']);
		}
		if(isset($attr['class'])&&is_array($attr['class'])) {
			$attr['class'] = $this->arrayToString($attr['class']);
		}
		if(isset($attr['style'])&&is_array($attr['style'])) {
			$attr['style'] = $this->arrayToCSS($attr['style']);
		}
		
		$return = '<'.$name;
		
		foreach($attr as $attrName => $attrValue) {
			if($attr!==false) {	
				if($attr===true) {
					$attrValue = $attrName;
				}
				$return .= ' '.$attrName.'="'.$attrValue.'"';
			}
		}
		
		if($this->xhtml&&$args['selfClosing']) {
			$return .= ' />';
		} else if(isset($args['content'])) {
			$return .= '>'.$args['content'].'</'.$name.'>';
		} else {
			$return .= '>';
			$this->opened = arrayAppend($this->opened, array($name));
		}
		
		
		return $return;
	}
	
	function close() {
		$name = end($this->opened);
		unset($this->opened[key($this->opened)]);
		return '</'.$name.'>';
	}

	private function arrayToString($arr) {
		return implode(' ', $arr);
	}

	private function arrayToCSS($arr) {
		$str = '';
		foreach($arr as $key => $value) {
			$str .= $key.': '.$value.';';
		}
		return $str;
	}
}

?>
